//
//  MineHeadView.m
//  GodCoin
//
//  Created by Bishen on 2018/6/6.
//  Copyright © 2018年 xiaohan. All rights reserved.
//

#import "MineHeadView.h"

@implementation MineHeadView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    [self.contentView addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(3));
        make.left.mas_equalTo(AdaptedWidth(20));
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(65), AdaptedWidth(65)));
    }];
    [self.contentView  addSubview:self.editBtn];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.iconView.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-AdaptedWidth(13));
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [self.contentView  addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.iconView.mas_centerY);
        make.left.equalTo(self.iconView.mas_right).offset(AdaptedWidth(15));
        make.right.equalTo(self.editBtn.mas_left).offset(AdaptedWidth(-15));
    }];
    kWeakSelf(self);
    [self.editBtn addTapBlock:^(UIButton * _Nonnull btn) {
        !weakself.EditClick?:weakself.EditClick();
    }];
    
    self.iconView.contentMode = UIViewContentModeScaleAspectFit;
    ViewRadius(self.iconView, kPortraitImageViewCornerRadius);
    self.userInteractionEnabled = YES;
    self.iconView.userInteractionEnabled = YES;
    self.nameLabel.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        !weakself.BackClick?:weakself.BackClick();
    }]];
}

- (void)setIsLogin:(BOOL)isLogin{
    _isLogin = isLogin;
    if (!isLogin) {
        self.iconView.image = kDefaultPortraitImage;
        self.nameLabel.text = XLocalized(@"未登录");
    }else{
        [self.iconView sd_setImageWithURL:String_To_URL(kCurUser.headimgurl) placeholderImage:kDefaultPortraitImage];
        self.nameLabel.text = kCurUser.nickname;
    }
    self.editBtn.hidden = !isLogin;
}

- (UIImageView*)iconView{
    if (!_iconView) {
        _iconView = [UIImageView new];
    }
    return _iconView;
}
- (UILabel*)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = kBoldFont(17);
    }
    return _nameLabel;
}
- (UIButton*)editBtn{
    if (!_editBtn) {
        _editBtn = [UIButton new];
        [_editBtn setImage:IMAGE_NAMED(@"pan_edit") forState:UIControlStateNormal];
    }
    return _editBtn;
}
@end
